{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 饼图 (Pie Chart) 教程\n",
        "\n",
        "本部分将详细讲解如何使用 Matplotlib 绘制饼图，包括基础饼图的绘制、饼图的定制选项以及环形图的应用。\n",
        "\n",
        "## 学习内容\n",
        "1. **基础饼图**\n",
        "   - `plt.pie(x, labels)` 绘制饼图\n",
        "   - `labels`：标签列表\n",
        "   - `autopct`：百分比格式字符串\n",
        "   - `startangle`：起始角度\n",
        "\n",
        "2. **饼图定制**\n",
        "   - `colors`：颜色列表\n",
        "   - `explode`：突出显示某些扇形\n",
        "   - `shadow`：阴影效果\n",
        "   - `wedgeprops`：扇形属性（边框、宽度等）\n",
        "\n",
        "3. **环形图（甜甜圈图）**\n",
        "   - `wedgeprops={'width': 0.5}`：创建环形图\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 导入必要的库\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "\n",
        "# 设置中文字体（如果需要显示中文）\n",
        "plt.rcParams['font.sans-serif'] = ['PingFang SC', 'Arial Unicode MS']\n",
        "plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题\n",
        "\n",
        "# 在 Jupyter Notebook 中内联显示图形\n",
        "%matplotlib inline\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 第一部分：基础饼图\n",
        "\n",
        "饼图用于展示数据的比例关系，每个扇形代表一个类别，扇形的大小表示该类别的占比。\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 1.1 plt.pie(x, labels) 绘制饼图\n",
        "\n",
        "`plt.pie(x, labels)` 是绘制饼图的基础函数。\n",
        "\n",
        "**基本语法**：\n",
        "```python\n",
        "plt.pie(x, labels)\n",
        "```\n",
        "\n",
        "**参数说明**：\n",
        "- `x`：数据值（数组或列表），表示每个扇形的数值\n",
        "- `labels`：标签列表，每个扇形的标签\n",
        "- `autopct`：百分比格式字符串，如 `'%1.1f%%'` 显示百分比\n",
        "- `startangle`：起始角度（度），默认从 x 轴正方向开始（0度）\n",
        "\n",
        "**返回值**：\n",
        "- `wedges`：扇形对象列表\n",
        "- `texts`：标签文本对象列表\n",
        "- `autotexts`：百分比文本对象列表（如果设置了 autopct）\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 示例 1：基础饼图\n",
        "categories = ['产品A', '产品B', '产品C', '产品D', '产品E']\n",
        "values = [23, 45, 56, 78, 32]\n",
        "\n",
        "plt.figure(figsize=(8, 8))\n",
        "plt.pie(values, labels=categories)\n",
        "plt.title('基础饼图示例', fontsize=14)\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 1.2 autopct 参数：显示百分比\n",
        "\n",
        "`autopct` 参数用于在饼图上显示百分比。\n",
        "\n",
        "**基本语法**：\n",
        "```python\n",
        "plt.pie(x, labels, autopct='%1.1f%%')  # 显示一位小数\n",
        "plt.pie(x, labels, autopct='%1.0f%%')  # 显示整数\n",
        "plt.pie(x, labels, autopct='%1.2f%%')  # 显示两位小数\n",
        "```\n",
        "\n",
        "**格式说明**：\n",
        "- `%1.1f%%`：显示一位小数的百分比\n",
        "- `%1.0f%%`：显示整数的百分比\n",
        "- `%1.2f%%`：显示两位小数的百分比\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 示例 2：显示百分比\n",
        "categories = ['产品A', '产品B', '产品C', '产品D', '产品E']\n",
        "values = [23, 45, 56, 78, 32]\n",
        "\n",
        "fig, axes = plt.subplots(1, 2, figsize=(14, 6))\n",
        "\n",
        "# 不显示百分比\n",
        "axes[0].pie(values, labels=categories)\n",
        "axes[0].set_title('不显示百分比', fontsize=12)\n",
        "\n",
        "# 显示百分比\n",
        "axes[1].pie(values, labels=categories, autopct='%1.1f%%')\n",
        "axes[1].set_title('显示百分比', fontsize=12)\n",
        "\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 1.3 startangle 参数：起始角度\n",
        "\n",
        "`startangle` 参数控制饼图的起始角度，可以旋转饼图。\n",
        "\n",
        "**基本语法**：\n",
        "```python\n",
        "plt.pie(x, labels, startangle=90)  # 从90度（顶部）开始\n",
        "```\n",
        "\n",
        "**角度说明**：\n",
        "- 0度：从右侧（3点钟方向）开始（默认）\n",
        "- 90度：从顶部（12点钟方向）开始\n",
        "- 180度：从左侧（9点钟方向）开始\n",
        "- 270度：从底部（6点钟方向）开始\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 示例 3：不同起始角度\n",
        "categories = ['产品A', '产品B', '产品C', '产品D', '产品E']\n",
        "values = [23, 45, 56, 78, 32]\n",
        "\n",
        "fig, axes = plt.subplots(1, 2, figsize=(14, 6))\n",
        "\n",
        "# 默认起始角度（0度）\n",
        "axes[0].pie(values, labels=categories, autopct='%1.1f%%', startangle=0)\n",
        "axes[0].set_title('起始角度 0°（默认）', fontsize=12)\n",
        "\n",
        "# 从顶部开始（90度）\n",
        "axes[1].pie(values, labels=categories, autopct='%1.1f%%', startangle=90)\n",
        "axes[1].set_title('起始角度 90°', fontsize=12)\n",
        "\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 第二部分：饼图定制\n",
        "\n",
        "饼图支持丰富的定制选项，可以控制颜色、突出显示、阴影等。\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 2.1 colors 参数：颜色列表\n",
        "\n",
        "`colors` 参数用于指定每个扇形的颜色。\n",
        "\n",
        "**基本语法**：\n",
        "```python\n",
        "plt.pie(x, labels, colors=['red', 'blue', 'green', 'orange', 'purple'])\n",
        "```\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 示例 4：不同颜色\n",
        "categories = ['产品A', '产品B', '产品C', '产品D', '产品E']\n",
        "values = [23, 45, 56, 78, 32]\n",
        "colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8']\n",
        "\n",
        "fig, axes = plt.subplots(1, 2, figsize=(14, 6))\n",
        "\n",
        "# 默认颜色\n",
        "axes[0].pie(values, labels=categories, autopct='%1.1f%%')\n",
        "axes[0].set_title('默认颜色', fontsize=12)\n",
        "\n",
        "# 自定义颜色\n",
        "axes[1].pie(values, labels=categories, autopct='%1.1f%%', colors=colors)\n",
        "axes[1].set_title('自定义颜色', fontsize=12)\n",
        "\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 2.2 explode 参数：突出显示\n",
        "\n",
        "`explode` 参数用于突出显示某些扇形，将其从饼图中分离出来。\n",
        "\n",
        "**基本语法**：\n",
        "```python\n",
        "explode = (0, 0.1, 0, 0, 0)  # 第二个扇形突出 0.1\n",
        "plt.pie(x, labels, explode=explode)\n",
        "```\n",
        "\n",
        "**参数说明**：\n",
        "- `explode` 是一个元组或数组，长度与数据相同\n",
        "- 每个值表示对应扇形分离的距离（相对于半径的比例）\n",
        "- 0 表示不分离，值越大分离越远\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 示例 5：突出显示\n",
        "categories = ['产品A', '产品B', '产品C', '产品D', '产品E']\n",
        "values = [23, 45, 56, 78, 32]\n",
        "colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8']\n",
        "\n",
        "fig, axes = plt.subplots(1, 2, figsize=(14, 6))\n",
        "\n",
        "# 不突出\n",
        "axes[0].pie(values, labels=categories, autopct='%1.1f%%', colors=colors)\n",
        "axes[0].set_title('不突出显示', fontsize=12)\n",
        "\n",
        "# 突出显示最大的扇形（产品D）\n",
        "explode = (0, 0, 0, 0.1, 0)  # 第四个扇形（产品D）突出\n",
        "axes[1].pie(values, labels=categories, autopct='%1.1f%%', colors=colors, explode=explode)\n",
        "axes[1].set_title('突出显示最大扇形', fontsize=12)\n",
        "\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 2.3 shadow 参数：阴影效果\n",
        "\n",
        "`shadow` 参数用于为饼图添加阴影效果，增加立体感。\n",
        "\n",
        "**基本语法**：\n",
        "```python\n",
        "plt.pie(x, labels, shadow=True)  # 添加阴影\n",
        "```\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 示例 6：阴影效果\n",
        "categories = ['产品A', '产品B', '产品C', '产品D', '产品E']\n",
        "values = [23, 45, 56, 78, 32]\n",
        "colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8']\n",
        "\n",
        "fig, axes = plt.subplots(1, 2, figsize=(14, 6))\n",
        "\n",
        "# 无阴影\n",
        "axes[0].pie(values, labels=categories, autopct='%1.1f%%', colors=colors)\n",
        "axes[0].set_title('无阴影', fontsize=12)\n",
        "\n",
        "# 有阴影\n",
        "axes[1].pie(values, labels=categories, autopct='%1.1f%%', colors=colors, shadow=True)\n",
        "axes[1].set_title('有阴影', fontsize=12)\n",
        "\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 2.4 wedgeprops 参数：扇形属性\n",
        "\n",
        "`wedgeprops` 参数用于设置扇形的属性，如边框、宽度等。\n",
        "\n",
        "**基本语法**：\n",
        "```python\n",
        "plt.pie(x, labels, wedgeprops={'edgecolor': 'black', 'linewidth': 2})\n",
        "```\n",
        "\n",
        "**常用属性**：\n",
        "- `edgecolor`：边框颜色\n",
        "- `linewidth`：边框线宽\n",
        "- `width`：扇形宽度（用于创建环形图）\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 示例 7：扇形属性\n",
        "categories = ['产品A', '产品B', '产品C', '产品D', '产品E']\n",
        "values = [23, 45, 56, 78, 32]\n",
        "colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8']\n",
        "\n",
        "fig, axes = plt.subplots(1, 2, figsize=(14, 6))\n",
        "\n",
        "# 无边框\n",
        "axes[0].pie(values, labels=categories, autopct='%1.1f%%', colors=colors)\n",
        "axes[0].set_title('无边框', fontsize=12)\n",
        "\n",
        "# 有边框\n",
        "axes[1].pie(values, labels=categories, autopct='%1.1f%%', colors=colors, \n",
        "            wedgeprops={'edgecolor': 'black', 'linewidth': 2})\n",
        "axes[1].set_title('有边框', fontsize=12)\n",
        "\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 第三部分：环形图（甜甜圈图）\n",
        "\n",
        "环形图是饼图的变体，通过设置 `wedgeprops` 的 `width` 参数创建。\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 3.1 使用 wedgeprops 创建环形图\n",
        "\n",
        "环形图通过设置 `wedgeprops={'width': 0.5}` 创建，其中 `width` 值表示环的宽度（相对于半径的比例）。\n",
        "\n",
        "**基本语法**：\n",
        "```python\n",
        "plt.pie(x, labels, wedgeprops={'width': 0.5})  # 创建环形图\n",
        "```\n",
        "\n",
        "**参数说明**：\n",
        "- `width` 的取值范围为 0 到 1\n",
        "- `width=0`：完整的饼图\n",
        "- `width=0.5`：中等宽度的环形图\n",
        "- `width` 越大，环越窄\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 示例 8：环形图\n",
        "categories = ['产品A', '产品B', '产品C', '产品D', '产品E']\n",
        "values = [23, 45, 56, 78, 32]\n",
        "colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8']\n",
        "\n",
        "fig, axes = plt.subplots(1, 3, figsize=(18, 6))\n",
        "\n",
        "# 完整饼图\n",
        "axes[0].pie(values, labels=categories, autopct='%1.1f%%', colors=colors)\n",
        "axes[0].set_title('完整饼图 (width=0)', fontsize=12)\n",
        "\n",
        "# 中等宽度环形图\n",
        "axes[1].pie(values, labels=categories, autopct='%1.1f%%', colors=colors,\n",
        "            wedgeprops={'width': 0.5, 'edgecolor': 'white', 'linewidth': 2})\n",
        "axes[1].set_title('中等宽度环形图 (width=0.5)', fontsize=12)\n",
        "\n",
        "# 窄环形图\n",
        "axes[2].pie(values, labels=categories, autopct='%1.1f%%', colors=colors,\n",
        "            wedgeprops={'width': 0.3, 'edgecolor': 'white', 'linewidth': 2})\n",
        "axes[2].set_title('窄环形图 (width=0.3)', fontsize=12)\n",
        "\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### 3.2 综合案例：完整的饼图定制\n",
        "\n",
        "结合所有定制选项，创建一个美观的饼图。\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# 示例 9：综合案例\n",
        "categories = ['产品A', '产品B', '产品C', '产品D', '产品E']\n",
        "values = [23, 45, 56, 78, 32]\n",
        "colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A', '#98D8C8']\n",
        "explode = (0, 0, 0, 0.1, 0)  # 突出显示最大的扇形\n",
        "\n",
        "plt.figure(figsize=(10, 8))\n",
        "plt.pie(values, labels=categories, autopct='%1.1f%%', colors=colors, \n",
        "        explode=explode, shadow=True, startangle=90,\n",
        "        wedgeprops={'edgecolor': 'white', 'linewidth': 2})\n",
        "plt.title('综合案例：完整的饼图定制', fontsize=14, pad=20)\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 总结\n",
        "\n",
        "本教程详细介绍了 Matplotlib 饼图的绘制方法，包括：\n",
        "\n",
        "1. **基础饼图**：使用 `plt.pie(x, labels)` 绘制，支持 autopct、startangle 等参数\n",
        "2. **饼图定制**：颜色、突出显示、阴影、边框等选项\n",
        "3. **环形图**：使用 `wedgeprops={'width': 0.5}` 创建环形图\n",
        "\n",
        "**关键要点**：\n",
        "- 饼图用于展示数据的比例关系\n",
        "- `autopct` 参数用于显示百分比\n",
        "- `explode` 参数可以突出显示某些扇形\n",
        "- `wedgeprops` 参数可以设置扇形的属性，包括创建环形图\n",
        "- 环形图通过设置 `width` 参数实现，值越大环越窄\n"
      ]
    }
  ],
  "metadata": {
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
